<template>
  <div class="score-analysis-container">
    <el-card class="score-card">
      <div class="card-header">
        <div class="header-title">
          <el-icon><DataAnalysis /></el-icon>
          <h2>考试成绩分析与导出</h2>
        </div>
        
        <div class="filter-container">
          <div class="filter-item-wrapper">
            <span class="filter-label">时间排序：</span>
            <el-select 
              v-model="timeOrder" 
              placeholder="时间排序" 
              class="filter-select"
            >
              <el-option label="时间降序" value="desc" />
              <el-option label="时间升序" value="asc" />
            </el-select>
          </div>
          
          <div class="button-group">
            <el-button 
              @click="handleReset" 
              class="reset-button"
              plain
            >
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
            
            <el-button 
              type="primary" 
              @click="handleQuery" 
              class="query-button"
            >
              <el-icon><Search /></el-icon>
              查询
            </el-button>
          </div>
        </div>
      </div>
      
      <div class="table-container">
        <page-list
          :tableData="examList"
          :total="total"
          @getPage="getPage"
        >
          <el-table-column label="序号" type="index" width="80" />
          <el-table-column label="考试名称" prop="examinationName">
            <template #default="{ row }">
              <div class="exam-name">
                <el-icon><Reading /></el-icon>
                <span>{{ row.examinationName }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="考试类型" prop="examinationType">
            <template #default="{ row }">
              <el-tag 
                :type="getExamTypeTagType(row.examinationType)" 
                effect="plain" 
                size="small"
              >
                {{ row.examinationType }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="结束时间" prop="endTime">
            <template #default="{ row }">
              <div class="time-cell">
                <el-icon><Calendar /></el-icon>
                <span>{{ formatTime(row.endTime) }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="240">
            <template #default="scope">
              <div class="action-buttons">
                <el-button 
                  type="primary" 
                  @click="goToScoreAnalysis(scope.row)"
                  class="action-button analysis-button"
                >
                  <el-icon><TrendCharts /></el-icon>
                  成绩分析
                </el-button>
                <el-button 
                  type="success" 
                  @click="openExportDialog(scope.row)"
                  class="action-button export-button"
                >
                  <el-icon><Download /></el-icon>
                  导出成绩
                </el-button>
              </div>
            </template>
          </el-table-column>
        </page-list>
      </div>
    </el-card>
  </div>

  <!-- 导出成绩弹窗 -->
  <el-dialog
    v-model="exportDialogVisible"
    title="导出成绩"
    width="500px"
    :close-on-click-modal="false"
    destroy-on-close
    class="export-dialog"
  >
    <div class="export-content">
      <div class="export-header">
        <div class="title-icon">
          <el-icon><DocumentCopy /></el-icon>
          <span>请选择要导出的班级</span>
        </div>
        <div class="select-actions">
          <el-button 
            type="primary" 
            plain 
            size="small" 
            @click="selectAllClasses"
            class="select-all-btn"
          >
            <el-icon><Select /></el-icon>
            全选班级
          </el-button>
        </div>
      </div>
      
      <div class="class-selection">
        <el-select
          v-model="selectedClasses"
          multiple
          collapse-tags
          collapse-tags-tooltip
          placeholder="请选择班级"
          class="class-select"
        >
          <el-option
            v-for="item in classesInfo"
            :key="item.classesId"
            :label="item.classesName"
            :value="item.classesId"
          >
            <div class="option-item">
              <span>{{ item.classesName }}</span>
            </div>
          </el-option>
        </el-select>
      </div>
      
      <div class="selected-info">
        <div class="info-item">
          <div class="info-label">已选班级数:</div>
          <div class="info-value">{{ selectedClasses.length }}</div>
        </div>
        <div class="info-item" v-if="classesInfo.length > 0">
          <div class="info-label">总班级数:</div>
          <div class="info-value">{{ classesInfo.length }}</div>
        </div>
      </div>
      
      <div class="export-tips">
        <el-alert
          title="导出说明"
          type="info"
          :closable="false"
          show-icon
        >
          <div class="tips-content">
            导出的成绩将以Excel格式保存，包含所选班级的学生成绩信息。
          </div>
        </el-alert>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="exportDialogVisible = false" class="cancel-btn">
          <el-icon><Close /></el-icon>
          取消
        </el-button>
        <el-button 
          type="success" 
          @click="exportScore" 
          :disabled="selectedClasses.length === 0"
          class="confirm-btn"
        >
          <el-icon><Download /></el-icon>
          开始导出
        </el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useScoreAnalysisExport } from '@/hooks/server/useScoreAnalysisExport'; 
import { 
  DataAnalysis, 
  Reading, 
  Calendar, 
  TrendCharts, 
  Download, 
  Refresh,
  Search,
  DocumentCopy,
  Select,
  Close,
  School
} from '@element-plus/icons-vue';

const { 
  examList,
  total,
  page,
  pageSize,
  timeOrder,
  getExamList,
  handleReset,
  handleQuery,

  // 班级信息
  classesInfo,
  examinationId,
  getClassesInfo,

  // 导出弹窗
  openExportDialog,
  exportDialogVisible,

  // 导出成绩
  selectedClasses,
  selectAllClasses,
  exportScore,

  // 跳转到成绩分析
  goToScoreAnalysis
} = useScoreAnalysisExport()

// 根据考试类型获取不同的标签类型
const getExamTypeTagType = (type) => {
  const typeMap = {
    '期中考试': 'warning',
    '期末考试': 'danger',
    '模拟考试': 'info',
    '单元测试': 'success'
  };
  return typeMap[type] || 'primary';
}

// 格式化时间显示
const formatTime = (timestamp) => {
  if (!timestamp) return '未设置';
  
  try {
    const date = new Date(timestamp);
    return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
  } catch (e) {
    return timestamp;
  }
}





const getPage = (pageParams) => {
  page.value = pageParams.page
  pageSize.value = pageParams.pageSize
  getExamList()
}

onMounted(async () => {
  await getExamList()
})
</script>

<style scoped lang="scss">
.score-analysis-container {
  padding: 16px;
  max-width: 1200px;
  margin: 0 auto;
}

.score-card {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  border-radius: 10px;
  overflow: hidden;
  transition: all 0.3s ease;
  
  &:hover {
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
  }
}

.card-header {
  margin-bottom: 20px;
}

.header-title {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  
  .el-icon {
    font-size: 22px;
    color: #409EFF;
    margin-right: 8px;
  }
  
  h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #303133;
  }
}

.filter-container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 8px;
  gap: 16px;
}

.filter-item-wrapper {
  display: flex;
  align-items: center;
  
  .filter-label {
    font-size: 14px;
    color: #606266;
    margin-right: 8px;
    white-space: nowrap;
  }
  
  .filter-select {
    width: 160px;
  }
}

.button-group {
  display: flex;
  gap: 12px;
  margin-left: auto;
}

.reset-button, .query-button {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  
  .el-icon {
    margin-right: 4px;
  }
}

.reset-button {
  border-color: #dcdfe6;
}

.query-button {
  background: linear-gradient(135deg, #409EFF, #1989fa);
  border: none;
  
  &:hover {
    background: linear-gradient(135deg, #66b1ff, #409EFF);
  }
}

.table-container {
  margin-top: 20px;
}

.exam-name, .time-cell {
  display: flex;
  align-items: center;
  
  .el-icon {
    margin-right: 6px;
    font-size: 16px;
    color: #909399;
  }
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.action-button {
  display: flex;
  align-items: center;
  padding: 6px 12px;
  
  .el-icon {
    margin-right: 4px;
  }
}

.analysis-button {
  background: linear-gradient(135deg, #409EFF, #1989fa);
  border: none;
  color: white;
  
  &:hover {
    background: linear-gradient(135deg, #66b1ff, #409EFF);
  }
}

.export-button {
  background: linear-gradient(135deg, #67c23a, #4caf50);
  border: none;
  color: white;
  
  &:hover {
    background: linear-gradient(135deg, #85ce61, #67c23a);
  }
}

:deep(.el-table) {
  --el-table-border-color: #ebeef5;
  --el-table-header-bg-color: #f5f7fa;
  border-radius: 8px;
  overflow: hidden;
  
  .el-table__header th {
    background-color: #f5f7fa;
    color: #606266;
    font-weight: 600;
  }
  
  .el-table__row {
    transition: all 0.2s ease;
    
    &:hover {
      background-color: #f0f7ff !important;
    }
  }
}

:deep(.el-pagination) {
  margin-top: 20px;
  justify-content: center;
}

@media (max-width: 768px) {
  .filter-container {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .filter-item-wrapper {
    width: 100%;
    margin-bottom: 12px;
    
    .filter-select {
      width: 100%;
    }
  }
  
  .button-group {
    width: 100%;
    margin-left: 0;
    justify-content: flex-end;
  }
  
  .action-buttons {
    flex-direction: column;
  }
}

.export-dialog {
  :deep(.el-dialog__header) {
    padding: 20px;
    margin-right: 0;
    border-bottom: 1px solid #f0f0f0;
    
    .el-dialog__title {
      font-size: 18px;
      font-weight: 600;
      color: #303133;
    }
  }
  
  :deep(.el-dialog__body) {
    padding: 0;
  }
  
  :deep(.el-dialog__footer) {
    padding: 15px 20px;
    border-top: 1px solid #f0f0f0;
  }
  
  .export-content {
    padding: 20px;
  }

  .export-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;

    .title-icon {
      display: flex;
      align-items: center;

      .el-icon {
        font-size: 22px;
        color: #409EFF;
        margin-right: 8px;
      }

      span {
        font-size: 16px;
        font-weight: 600;
        color: #303133;
      }
    }

    .select-actions {
      display: flex;
      gap: 12px;
      
      .select-all-btn {
        .el-icon {
          margin-right: 4px;
        }
      }
    }
  }

  .class-selection {
    margin-bottom: 20px;
    
    .class-select {
      width: 100%;
    }
    
    :deep(.el-select__tags) {
      background-color: #f8f9fa;
      border-radius: 4px;
      padding: 4px;
      margin-bottom: 4px;
    }
  }
  
  .option-item {
    display: flex;
    align-items: center;
    
    .el-icon {
      margin-right: 6px;
      color: #409EFF;
    }
  }

  .selected-info {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;
    padding: 12px 16px;
    background-color: #f8f9fa;
    border-radius: 6px;

    .info-item {
      display: flex;
      align-items: center;

      .info-label {
        font-size: 14px;
        color: #606266;
        margin-right: 8px;
      }

      .info-value {
        font-size: 14px;
        font-weight: 600;
        color: #409EFF;
      }
    }
  }

  .export-tips {
    margin-bottom: 0;
    
    .tips-content {
      font-size: 13px;
      color: #909399;
      line-height: 1.5;
    }
  }

  .dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
  }

  .cancel-btn, .confirm-btn {
    display: flex;
    align-items: center;
    padding: 9px 20px;

    .el-icon {
      margin-right: 4px;
      font-size: 16px;
    }
  }

  .cancel-btn {
    border-color: #dcdfe6;
  }

  .confirm-btn {
    background: linear-gradient(135deg, #67c23a, #4caf50);
    border: none;
    
    &:hover {
      background: linear-gradient(135deg, #85ce61, #67c23a);
    }
    
    &:disabled {
      background: #c0c4cc;
      color: #fff;
    }
  }
}
</style>

